package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - logs user out from the system
 * 
 * @author Saint
 * 
 */
public class LogoutCommand extends Command {

	private static final long serialVersionUID = -1097155413385971607L;
	private static final Logger log = Logger.getLogger(LogoutCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		HttpSession session = request.getSession(false);
		if (session != null)
			session.invalidate();

		log.debug("Command finished");

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.login"));
		request.setAttribute("next", Path.PAGE_LOGIN);
		String forward = Path.PAGE_SUCCESS;

		return forward;
	}
}
